//
//  SecondViewController.m
//  StreamVideoDemo
//
//  Created by DaveLam on 15/1/6.
//  Copyright (c) 2015年 DaveLam. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
@synthesize dataArray=_dataArray;
@synthesize iconArray=_iconArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _dataArray=[[NSMutableArray alloc] initWithObjects:@"个人中心",@"我的课程",@"我的收藏",@"播放历史",@"退出", nil];
    _iconArray=[[NSMutableArray alloc] initWithObjects:@"",@"home_course_untap",@"home_collect_untap",@"home_history_untap",@"home_quit_untap", nil];
    //    int width=[[UIScreen mainScreen] bounds].size.width;
    //    int height=[[UIScreen mainScreen] bounds].size.height;
    //    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(70, 20, width, height)];
    //    [self.view addSubview:self.tableView];
    [self.tableView addObserver:self
                     forKeyPath:@"revealSideInset"
                        options:NSKeyValueObservingOptionNew
                        context:NULL];
    CGRect newFrame=self.tableView.frame;
    newFrame.origin.y+=20;
    self.tableView.frame=newFrame;
}
-(void)viewDidUnload{
    [super viewDidUnload];
    [self.tableView removeObserver:self
                        forKeyPath:@"revealSideInset"];
}
-(id)initWithStyle:(UITableViewStyle)style{
    self=[super initWithStyle:style];
    if (self) {
        [self.tableView setBackgroundColor:[UIColor colorWithRed:36/255.0 green:41/255.0 blue:48/255.0 alpha:0.95]];
        [self.tableView setSeparatorColor:[UIColor clearColor]];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"revealSideInset"]) {
        UIEdgeInsets newInset = self.tableView.contentInset;
        newInset.top = self.tableView.revealSideInset.top;
        newInset.bottom = self.tableView.revealSideInset.bottom;
        self.tableView.contentInset = newInset;
    } else [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)didReceiveMemoryWarning {
    @try {
        [self.tableView removeObserver:self
                            forKeyPath:@"revealSideInset"];
    } @catch (NSException *exception) {
    } @finally {
    }
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

-(void)dealloc{
    @try {
        [self.tableView removeObserver:self
                            forKeyPath:@"revealSideInset"];
    } @catch (id anException) {
        //do nothing, obviously it wasn't attached because an exception was thrown
    }
#if !PP_ARC_ENABLED
    [super dealloc];
#endif
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return toInterfaceOrientation==UIInterfaceOrientationPortrait;
}

//-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return [_titleArray objectAtIndex:section];
//}

//-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
//    return [_titleArray count];
//}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

//-(float) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 60;
//}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* currentCell;
    if (indexPath.row==0) {
        TitleCell* cell = (TitleCell*)[tableView dequeueReusableCellWithIdentifier:@"TitleCell"];
        if (cell==nil) {
            cell = [[TitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TitleCell"];
        }
        cell.revealSideInset=self.tableView.revealSideInset;
        [[cell titleLabel] setText:[_dataArray objectAtIndex:indexPath.row]];
        CGRect newFrame=cell.frame;
        newFrame.size.height=60;
        cell.frame=newFrame;
        currentCell=cell;
    }
    else{
        ContentCell* cell = (ContentCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
        
        if (cell == nil) {
            cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        cell.revealSideInset=self.tableView.revealSideInset;
        [[cell contentLabel] setText:[_dataArray objectAtIndex:indexPath.row]];
        [[cell icon] setImage:[UIImage imageNamed:[_iconArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
        if (indexPath.row==4) {
            [[cell infobtn] setAlpha:0];
        }
        [[cell infobtn] setTitle:@"1" forState:UIControlStateNormal];
        currentCell=cell;
    }
    return currentCell;
}

@end
